<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 商户控制器 - 多语言系统控制器 demo
 * 
 * @author 	FangMinglong
 * @date 	2017-04-12 17:51
 *
 * @desc    完善文档注释
 * @version 1.1.0
 * @date	2017年6月27日 下午5:26:04
 *
 * @title 商户控制器-商户相关功能
 * @entry http://discovery.segway.com
 */
class Merchant extends MY_Consumer_Controller
{
	// Must be public!!!
	/** @var Lib_merchant */
	public $lib_merchant = null;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('mod_merchant');
		$this->load->library('lib_merchant');
		
		// 加载语言文件，第二个可选参数 language 指定要加载哪种语言
		$this->lang->load('merchant'); 
	}
	
	/**
	 * 首页： 列表页功能
	 * 
	 * @return html
	 * 
	 */
	public function index()
	{
		$this->listing();
	}
	
	/**
	 * 商户列表页
	 *
	 * @method  GET
	 * @author  Jason
	 * @date	2017年7月17日 下午9:01:32
	 * @link    /merchant/listing
	 */
	public function listing($page = 1, $number = 2/*TODO 0*/)
	{
		$cookie_lang = $this->lib_cookie->get_language();
		$language = $this->lib_language->get_supported_language($this->language, $cookie_lang);
		
		$this->set_mutable_view_var('head.title', $this->lang->line('merchant_words_list_merchant'));
		
		$conditions = $this->lib_merchant->get_conditions();
		$list = $this->lib_merchant->get_merchants_for_list($conditions, $language, $page, $number);
		
		$data['list'] = $list;
		
		$this->load->view('consumer/merchant/index', $data);
	}
	
	/**
	 * 商户详情页
	 *
	 * @desc    商户详情页
	 * @author  Jason
	 * @date    2017年5月3日 下午5:27:34
	 * @param   number $id 商户ID
	 * 			把参数按顺序辍到 URL 之后，用 "/" 间隔
	 * @example /merchant/detail/106
	 * 
	 * link     /merchant/detail
	 * 
	 * @demoParam id 106
	 */
	public function detail($id)
	{
		$cookie_lang = $this->lib_cookie->get_language();
		$language = $this->lib_language->get_supported_language($this->language, $cookie_lang);
		
		$merchant = $this->lib_merchant->get_merchant_for_detail($id, $language);
		
		$this->set_mutable_view_var('head.title', $merchant['name']);
		$this->set_mutable_view_var('head.keywords', $merchant['abstract']);
		$this->set_mutable_view_var('head.description', $merchant['abstract']);
		
		$data['content']['merchant'] = $merchant;
		
		$this->load->library('lib_touroute');
		$conditions = [
				['where', "merchant_id=$id"],
		];
		$touroutes_list = $this->lib_touroute->get_touroutes_for_list($conditions, $language);
		$data['content']['touroutes'] = $touroutes_list;
		
		$this->load->view('consumer/merchant/detail', $data);
	}
	
	/**
	 * 搜索商户页
	 *
	 * @author  Jason
	 * @date	2017年5月18日 下午12:52:12
	 * @param   string $keyword 关键字
	 * @param   number $country 国家ID，默认为 0
	 * @param   number $city    城市ID，默认为 0
	 * @param   string $language 默认为 "english"
	 * 			参数以 QUERY STRING 的方式传递！
	 * @example 如果参数为 keyword=segwa order=default country=693000 city=86690000
	 * 			那么URL为 /merchant/search/?keyword=segwa&order=default&country=693000&city=86690000
	 *
	 * @link    /merchant/search
	 *
	 * @actionParamsUriProtocolRule query_string
	 * @demoParam keyword segwa
	 * @demoParam order default
	 * @demoParam country 693000
	 * @demoParam city 86690000
	 */
	public function search(/* $keyword, $order = 'default', $country = 0, $city = 0, $language = '' */)
	{
		// 先接参数
		$keyword = $this->input->get('keyword') 	OR $keyword = '';
		$order = $this->input->get('order') 		OR $order = 'default';
		$country = $this->input->get('country') 	OR $country = 0;
		$city = $this->input->get('city') 			OR $city = 0;
		$language = $this->input->get('language') 	OR $language = '';
		$page = $this->input->get('page') 			OR $page = 1;
		$number = $this->input->get('number') 		OR $number = 2;
		
		$cookie_lang = $this->lib_cookie->get_language();
		$language = $this->lib_language->get_supported_language($language, $cookie_lang);
		
		$this->load->library('lib_merchant');
		$resources = $this->lib_merchant->get_ids_for_autocomplete($country, $city);
		$resources[] = -1;
		
		$module = 'merchant';
		$search_by = ['name'];
		$resources = $this->lib_language->get_resources_for_search($keyword, $module, $resources, $language, $search_by);
		$resources[] = -1;
		
		$conditions = [['where_in', ['id', $resources]]];
		$merchantes = $this->lib_merchant->get_merchants_for_list($conditions, $language, $page, $number);
		
		$this->load->library('lib_touroute');
		foreach ($merchantes['data'] as &$merchant) {
			$conditions = [
					['select', 'id,cover,name'],
					['where', "merchant_id={$merchant['id']}"],
					['where', ["`status` & 1 > 0", null, false]],
			];
			$touroutes = $this->lib_touroute->get_touroutes_for_list($conditions, $language, 1, 3)['data'];
			
			foreach ($touroutes as &$touroute) {
				$module = $this->mod_touroute->get_multi_language_info('module', 'master');
				$field_names = ['name'];
				$touroute = $this->lib_language->get_merged_item_data_with_fields($touroute, $module, $language, $field_names);
			}
			unset($touroute);
			$merchant['touroutes'] = $touroutes;
		}
		unset($merchant);
		
		$this->load->library('lib_area');
		list($status, $countries) = $this->lib_area->list_children(0, $language);
		if ($country){
			list($status, $cities) = $this->lib_area->list_children($country, $language);
		}else{
			$cities = [];
		}
		$metrix = [
				'country' => $countries,
				'city' => $cities,
		];
		
		$data['content']['merchants'] = $merchantes;
		$data['content']['areas'] = $metrix;
		
		$this->load->view('consumer/merchant/search', $data);
	}
	
}